package crm.modefy2;

import java.util.Scanner;

import crm.modefy2.Data.Node;
/**
 * 对链表进行操作的类
 * @author ZHUAN4
 *
 * @param <T>
 */
public class DataHandle<T extends CustomerM> {

	/**
	 * 链表初始化函数
	 * @param initialT
	 */
	public  void  initCRM(T[] initialT,Data data){
		 
		@SuppressWarnings("unchecked")
		T[] temp=(T[]) InitialDataModefied.initialData();
		data.getHead().setCusInfo((CustomerM) temp[0]);
		data.setPointer(data.getHead());
	   	for(int i=1;i<temp.length;i++){
			Node tempNode=(new Data()).new Node(temp[i]);
			data.getPointer().setNext(tempNode);
			tempNode.setPrev(data.getPointer());
			data.setPointer(data.getPointer().getNext());
//			pointer.setNext(tempNode);
//			tempNode.setPrev(pointer);
//			pointer=pointer.getNext(); 		
	   	}
	}
	public int getLengthCRM(Data data) {
		return data.getLength();
	}
	
	/**
	* get the num of node line at site of index
	* @param head
	* @param index
	* @return
	* @throws Exception
	*/
	public Node getNodeOfIndex(int index,Data data)
	{
		int length=this.getLengthCRM(data);
		Node temp = data.getHead();
		try {
			if(index>length||index<0) 
			{
				   throw new Exception("out of index!");
			}
			for(int i=1;i<=index;i++)
			{
				temp=temp.getNext();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
			
	}
	
	/**
	 * 查看所有的客户的信息，对应 无参L命令
	 * @throws Exception
	 */
   public  void lookCRM(Data data) {
	    int length=this.getLengthCRM(data);
		int[] flags=new int[length];
		for(int i=0;i<flags.length;i++)
		{
			flags[i]=i;
		}
        this.printCustomer(flags,data);
   }
   
   /**
	 * 通过顾客的id来查看顾客的信息，对应有参的L ID命令
	 * @param id
	 * @throws Exception
	 */
	public  void lookByIdCRM(int id,Data data) {
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM(data);i++)
		{
			if(this.getNodeOfIndex(i,data).getCusInfo().getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else
		{
			if(index<0&&index>=this.getLengthCRM(data))
			{
				System.out.println("The index found out of limit!");
			}
			else
			{
				int[] flags=new int[1];
				flags[0]=index;
				this.printCustomer(flags,data);
			}
		}
	}

	/**
	 * 通过顾客的name来查看顾客的信息，对应有参的L ID命令
	 * @param id
	 * @throws Exception
	 */
	public  void lookByNameCRM(String name,Data data) {
		int flag=0;
		int[] indexs = new int[this.getLengthCRM(data)];
		int count=0;
		for(int i=0;i<this.getLengthCRM(data);i++)
		{
			if(this.getNodeOfIndex(i,data).getCusInfo().getName().equals(name))
			{
				flag=1;
				indexs[count]=i;
				count++;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else
		{   
			int[] flags=new int[count];
			for(int i=0;i<count;i++)
			{
				flags[i]=indexs[i];
			}
			this.printCustomer(flags,data);
		}
	}
	
	/**
	 * 将target 中的值赋值给 head
	 * @param head
	 * @param target
	 */
	public void exchangeCRM(T source, T target){
		source.setAddress(target.getAddress());
		source.setBirthday(target.getBirthday());
		source.setCall(target.getCall());
		source.setName(target.getName());
		source.setSex(target.getSex());
		source.setId(target.getId());
	}
	
	/**
	 * 向信息中添加一条数据  对应 A 操作
	 * @param t
	 */
	public void  addCRM(T t,Data data){
		if(this.getLengthCRM(data)==0)
		{
			Node nodeTemp=(new Data()).new Node(t);
			data.setHead(nodeTemp);
			data.setPointer(data.getHead());
//			pointer=head;
			CustomerM.count++;
			int temp=data.getLength()+1;
			data.setLength(temp);
//			length++;
		}
		else if(this.getLengthCRM(data)>=1)
		{
			T cusTemp=t;
			Node nodeTemp=(new Data()).new Node(cusTemp);
			data.getPointer().setNext(nodeTemp);
			nodeTemp.setPrev(data.getPointer());
			data.setPointer(data.getPointer().getNext());
//			pointer.setNext(nodeTemp);
//			nodeTemp.setPrev(pointer);
//			pointer=pointer.getNext();
			CustomerM.count++;
			int temp=data.getLength()+1;
			data.setLength(temp);;
//			length++;
		}
	}
	
	/**
	 * 通过id来在链表中查找删除相应的信息
	 * @param id
	 * @throws Exception
	 */
	public void deleteCRM(int id,Data data) {
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM(data);i++)
		{
			if(this.getNodeOfIndex(i,data).getCusInfo().getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==1)
		{
			if(index>=0&&index<this.getLengthCRM(data))
			{
				System.out.println("Are you sure to delete this information ! Input your choose? Y/N");
				Scanner in=new Scanner(System.in);
				String choose=in.nextLine();
				if(choose.charAt(0)=='Y'||choose.charAt(0)=='y')
				{
					if(index==0)
					{
						data.setHead(data.getHead().getNext());
						data.getHead().setPrev(null);
//						head=head.getNext();
//						head.setPrev(null);
						int temp=data.getLength()-1;
						data.setLength(temp);
//						length--;
					}
					else if(index==this.getLengthCRM(data)-1)
					{
						data.setPointer(data.getPointer().getPrev());
						data.getPointer().setNext(null);
//						pointer=pointer.getPrev();
//						pointer.setNext(null);
						int temp=data.getLength()-1;
						data.setLength(temp);
//						length--;
					}
					else
					{
						Node temp=this.getNodeOfIndex(index,data);
						temp.getPrev().setNext(temp.getNext());
						temp.getNext().setPrev(temp.getPrev());
						temp.setNext(null);
						temp.setPrev(null);
						int tempI=data.getLength()-1;
						data.setLength(tempI);
//						length--;
					}
				}
				else
				{
					System.out.println("You choose to cancal your Delete operater!");
				}
			}
			else
			{
				System.out.println("Index found out of limit!");
			}
		}
		else
		{
			System.out.printf("The customer(id of %s) is not found!",id);
			System.out.println();
		}
					
	}
	
	/**
	 * 更新数据操作根据Id来查找信息并更新为update参数中的内容
	 * @param id
	 * @param update
	 * @throws Exception
	 */
	public void updateCRM(int id,T update,Data data) {
		
		int flag=0;//flag=1 find  flag=0  not find
		int index=-1;
		for(int i=0;i<this.getLengthCRM(data);i++)
		{
			if(this.getNodeOfIndex(i,data).getCusInfo().getId()==id)
			{
				flag=1;
				index=i;
			}
		}
		if(flag==0)
		{
			System.out.println("The information of customer who you want to modify if not found!");
		}
		else{
			if(index>=0&&index<this.getLengthCRM(data))
			{
				int tempId=this.getNodeOfIndex(index,data).getCusInfo().getId();
				this.exchangeCRM((T) this.getNodeOfIndex(index,data).getCusInfo(), update);
				this.getNodeOfIndex(index,data).getCusInfo().setId(tempId);
			}
			else
			{
				System.out.println("Index found out of limit!");
			}
		}	
	}
	
	/**
	 * 根据Id的大小排序,只做试视图
	 * @throws Exception
	 */
	public void sortByIdCRM(Data data) {
		int length=this.getLengthCRM(data);
		int[] flags=new int[length];
		for(int i=0;i<flags.length;i++)
		{
			flags[i]=i;
		}
		for(int i=length;i>1;i--)
		{
			for(int j=0;j<i-1;j++)
			{
				if(this.getNodeOfIndex(flags[j],data).getCusInfo().getId()<this.getNodeOfIndex(flags[j+1],data).getCusInfo().getId())
//				if(this.getNodeOfIndex(j).getCusInfo().getId()>this.getNodeOfIndex(j+1).getCusInfo().getId())
				{
					int temp=flags[j];
					flags[j]=flags[j+1];
					flags[j+1]=temp;
				}
			}
		}
		System.out.println("·······················Sort by ID·····························");
		this.printCustomer(flags,data);
	}
	
	/**
	 * 根据name的大小排序
	 * @throws Exception
	 */
	public void sortByNameCRM(Data data) {

		int length=this.getLengthCRM(data);
		int[] flags=new int[length];
		for(int i=0;i<flags.length;i++)
		{
			flags[i]=i;
		}
		for(int i=length;i>1;i--)
		{
			for(int j=0;j<i-1;j++)
			{
				if(this.getNodeOfIndex(flags[j],data).getCusInfo().getName().compareTo(this.getNodeOfIndex(flags[j+1],data).getCusInfo().getName())>0)
				{
					int temp=flags[j];
					flags[j]=flags[j+1];
					flags[j+1]=temp;
				}
			}
		}
		System.out.println("·······················Sort by Name···························");
		this.printCustomer(flags,data);
	}
	
	/**
	 * 打印控制函数，按照flags数组中的顺序打印出CustomerT的信息
	 * @param flags 记录顺序的数组
	 * @throws Exception 跑出数组月结异常
	 */
	public void printCustomer(int[] flags,Data data){
		
		System.out.println(" id        name    sex    birthday     call           address ");
	   	for(int i=0;i<flags.length;i++){
					System.out.printf("%3s",this.getNodeOfIndex(flags[i],data).getCusInfo().getId());
					System.out.printf("%12s",this.getNodeOfIndex(flags[i],data).getCusInfo().getName());
					System.out.printf("%6s   ",this.getNodeOfIndex(flags[i],data).getCusInfo().getSex());
					System.out.printf("%10s",this.getNodeOfIndex(flags[i],data).getCusInfo().getBirthday());
					System.out.printf("%15s     ",this.getNodeOfIndex(flags[i],data).getCusInfo().getCall());
					System.out.printf("%-100s",this.getNodeOfIndex(flags[i],data).getCusInfo().getAddress());
					System.out.println();
	   	}
	}

}
